<#include "/common/head.html"/>
<script type="text/javascript" src="${basePath}/js/ZeroClipboard/ZeroClipboard.min.js"></script>
<div class="easyui-layout" data-options="fit:true, border:false">
	<div data-options="region:'center', border:false">
	    <table id="dg"></table>
	    <div id="tb" style="padding:2px 5px;">
	        <div class="wrap_search">
	            <span class="search_item">
	                <span class="item_text">菜单名称: </span>
	                <span class="item_obj">
	                    <input class="easyui-textbox" type="text" name="form_name" />
	                </span>    
	            </span>
	        </div>
	        <div style="text-align: left; margin:6px;">
	            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add()">增加</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="update()">编辑</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-clear" plain="true" onclick="del()">删除</a>
	            <div style="float:right; padding-right:40px;">
	                <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="zcurdSearch(this)">搜索</a>
	                <a href="#" class="easyui-linkbutton" iconCls="icon-search">重置</a>
	            </div>
	        </div>
	    </div>
    </div>
    <div data-options="region:'east',split:true, border:false" title="按钮列表" style="width:40%">
    	<iframe id="subIframe" class="easyui-panel" data-options="fit:true,border:false"></iframe>
    </div>
</div>    
<script type="text/javascript">
var datagrid = $("#dg");
$(function() {
	//显示列表
	datagrid.treegrid({
		idField: 'id',
        treeField: 'menu_name',
		rownumbers:true,
    	fit:true,
    	border:false,
    	rownumbers:true,
    	url: "${basePath}/menu/listAll",
    	method:'get',
    	toolbar:'#tb',
    	singleSelect:false,
    	columns:[[
	    	{field:'id', checkbox:true},
	    	{field:'menu_name', title: '菜单名称', width:140},
	    	{field:'menu_url', title: '菜单地址', width:120},
	        {field:'icon', title: '图标', width:100},
	        {field:'operate', title: '操作', width:100, formatter: 
	        	function(value, row, index){
				  	return '<a class="operate" href="###" onclick="menuBtnPage(this)">按钮列表</a>'; 
			  	}
	        }
	    ]],
	    loadFilter: function(data) {
	    	//转换数据
			var menulist = data;	
			for(var i = 0; i < menulist.length; i++) {
				var children = menulist[i].children || [];
				for(var j = 0; j < menulist.length; j++) {
					if(menulist[j].parent_id == menulist[i].id) {
						children.push(menulist[j]);
					}
				}
				if(children.length > 0) {
					menulist[i].children = children;
				}
			}
			var treeData = [];
			$.each(menulist, function(i, item) {
				if(item.parent_id == 0) {
					treeData.push(item);
				}
			});
			return treeData;
	    }
	});
});

//按钮管理
function menuBtnPage(obj) {
	var id = $(obj).parents("tr").find(":input[name='id']").val();
	$("#subIframe").attr("src", "${basePath}/zcurd/listPage?headId=136&menu_id=" + id);
	
}

//搜索
function zcurdSearch() {
	var param = zcurdGetParam();
	log(param);
	datagrid.datagrid("load", {
		queryParams: param
	})
}

function zcurdGetParam() {
	var param = {};
	$("#tb :input[name]").each(function(i, item) {
		if($(item).val()) {
			param[$(item).attr("name")] = $(item).val();	
		}
	});
	return param;
}

function add() {
	top.window.subPage.loadCurrDatagrid = function() {
		showMsg("增加成功！");
		datagrid.treegrid("load");
	}
	top.window.subPage.currDatagrid = datagrid;
	top.openWindow("增加", "${basePath}/menu/addPage", {width: 600, heigth: 400});
}

function update() {
	var rowsSel = datagrid.datagrid("getSelections");
	if(rowsSel.length != 1) {
		showWarnMsg("请选择需要编辑的一条数据！");
		return;
	}
	top.window.subPage.loadCurrDatagrid = function() {
		showMsg("更新成功！");
		datagrid.treegrid("load");
	}
	var id = rowsSel[0].id;
	top.openWindow("编辑", "${basePath}/menu/updatePage?id=" + id);
}

function del() {
	var ids = [];
	var selRows = datagrid.datagrid("getSelections");
	for(var i=0; i<selRows.length; i++) {
		if(selRows[i].children) {
			showWarnMsg("不能删除目录！");
			return;
		}
		ids.push(selRows[i].id);
	}
	if(ids.length < 1) {
		showWarnMsg("请选择需要删除的数据！");
		return;
	}
	confirmMsg("确认删除？", function() {
		$.post("delete", {id: ids}, function(data) {
			showMsg("删除成功！");
			datagrid.treegrid("load");
		});
	});
}
</script>

<#include "/common/dialogWindow.html"/>
<#include "/common/foot.html"/>